<!--
 * @Author: daidai
 * @Date: 2022-02-28 16:29:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-09-27 15:05:45
 * @FilePath: \web-pc\src\pages\big-screen\components\echart\index.vue
-->
<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
import "echarts-gl";

function debounce(fn, delay) {
  let timer = null;
  return function (...args) {
    timer = setTimeout(
      () => {
        typeof fn === "function" && fn.apply(null, args);
        clearTimeout(timer);
      },
      delay > 0 ? delay : 100
    );
  };
}

export default {
  name: "echart",
  props: {
    renderType: {
      type: String,
      default: "svg",
    },
    className: {
      type: String,
      default: "chart",
    },
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    options: {
      type: Object,
      default: () => ({}),
    },
    mapJson: {
      type: Object,
      default: null,
    },
    // 是否随页面大小变化重新渲染图标
    isResize: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      chart: null,
      onResize: null,
    };
  },
  computed: {
    isTop(){
      return this.$store.state.setting.isTop
    }
  },
  watch: {
    options: {
      handler(options) {
        // 设置true清空echart缓存
        this.chart.setOption(options, true);
      },
      deep: true,
    },
    isTop(val){
      // console.log(val,'val');
      this.resize();
    }
  },
  mounted() {
    this.initChart();
    if (this.isResize) {
      this.onResize = debounce(() => {
        this.resize();
      }, 500);
      this.$nextTick(() => {
        this.resize();
        this.addListener();
      });
    }
  },
  beforeDestroy() {
    this.chart.dispose();
    this.chart = null;
    if (this.isResize){
      this.clearListener();
    }
  },
  methods: {
    initChart() {
      // 初始化echart
      if (this.mapJson) {
        echarts.registerMap("bd", this.mapJson);
        this.chart = echarts.init(this.$el);
      } else if (this.renderType === "svg") {
        // 一般图表都是svg渲染
        this.chart = echarts.init(this.$el, null, {
          renderer: "svg",
        });
      } else {
        // 3D饼图只能canvas渲染
        this.chart = echarts.init(this.$el);
      }

      this.chart.setOption(this.options, true);
    },
    addListener() {
      window.addEventListener("resize", this.onResize);
    },
    clearListener() {
      window.removeEventListener("resize", this.onResize);
    },
    resize() {
      if (!this.chart) {
        return;
      }
      this.chart.resize({
        animation: {
          duration: 500,
        },
      });
    },
  },
};
</script>

<style></style>
